package utilities.display.imgs;

import java.awt.AlphaComposite;
import java.awt.Composite;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;

import utilities.display.iface.Drawable;

public class DrawImage implements Drawable 
{
	protected BufferedImage baseImg;
	protected AlphaComposite compositeRule;
	
	protected double scale;
	protected int x;
	protected int y;
	
	public DrawImage(BufferedImage img, AlphaComposite composite, double scale, int x, int y)
	{
		this.baseImg = img;
		this.compositeRule = composite != null ? composite : AlphaComposite.SrcOver;
		this.scale = scale;
		this.x = x;
		this.y = y;
	}
	
	public void drawMe(Graphics g) 
	{
		if(baseImg != null)
		{
			Graphics2D g2 = (Graphics2D)g;
			
			// Get old settings
			Composite oldComp = g2.getComposite();
			AffineTransform oldTrans = g2.getTransform();
			
			// Set up transform
			AffineTransform newTrans = new AffineTransform();
			newTrans.scale(scale, scale);
			newTrans.translate(x / scale, y / scale);
			g2.setTransform(newTrans);
			
			// Set up opacity
			if(compositeRule != null) g2.setComposite(compositeRule);
			
			// Draw the image
			g2.drawImage(baseImg, newTrans, null);
			
			// Restore old settings
			g2.setComposite(oldComp);
			g2.setTransform(oldTrans);
			g2.scale(1, 1);
		}
	}
	
	public void translate(int dx, int dy)
	{
		x += dx;
		y += dy;
	}
	
	public void setComposite(AlphaComposite composite)
	{
		this.compositeRule = composite != null ? composite : AlphaComposite.SrcOver;
	}
	
	public void setScale(double scale)
	{
		this.scale = scale;
	}
}
